<template>
  <view>
    <view class="search-box">
      <!-- 使用 uni-ui 提供的搜索组件 -->
      <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
      <uni-list v-if="searchResult.length!==0">
        <uni-list-item :ellipsis="1" :to="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`" showArrow :title="item.goods_name" v-for="(item,index) in searchResult" :key="index" />
      </uni-list>
      <view class="history-box" v-else>
        <view class="history-title">
          <text>搜索历史</text>
          <uni-icons type="trash" size="17" @click="clearHistoryList"></uni-icons>
        </view>
        <view class="history-list">
          <uni-tag :text="item" v-for="(item,index) in historys" :key="index" @click="gotoGoodsList(item)"></uni-tag>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import {debounce} from 'lodash'
export default {
  name: '',
  components: {},
  data() {
    return {
      // 延时器的 timerId
      timer: null,
      // 搜索关键词
      kw: '',
      searchResult:[],
      historyList:uni.getStorageSync('kw') || []
    };
  },
  created() {},
  mounted() {},
  onLoad() {},
  computed:{
    historys(){
      return [...this.historyList].reverse()
    }
  },
  methods: {  
    // input(e) {
    //   // 清除 timer 对应的延时器
    //   clearTimeout(this.timer);
    //   // 重新启动一个延时器，并把 timerId 赋值给 this.timer
    //   this.timer = setTimeout(() => {
    //     // 如果 500 毫秒内，没有触发新的输入事件，则为搜索关键词赋值
    //     // console.log(e)
    //     this.kw = e;
    //     console.log(this.kw);
    //   }, 500);
    // }
    input:debounce(function(e){
      this.kw=e
      console.log('调用搜索方法',e)
      this.getSearchResult()
    },500),
    async getSearchResult(){
      if(this.kw.trim() === ''){
        this.searchResult = []
        return
      }
      let {data:{message,meta:{status}}} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
      console.log(message)
      // console.log(status)
      if(status!==200) return uni.$showMsg()
      this.searchResult=message
      this.saveSearchHistory()
    },
    saveSearchHistory(){
      // 方式1
      // let index = this.historyList.findIndex(t=>t===this.kw)
      // if(index>-1) this.historyList.splice(index,1)
      // this.historyList.push(this.kw)
      // 方式2
      // 把数组转化成set
      let s = new Set(this.historyList)
      // 删除可能重复内容
      s.delete(this.kw)
      // 添加内容
      s.add(this.kw)
      // 把set转化成数组
      console.log(s)
      this.historyList=[...s]
      // 通过es6,Array.from(伪数组/set)
      // this.historyList = Array.from(s)
      // 数据持久化
      uni.setStorage('kw',this.historyList)
    },
    clearHistoryList(){
      this.historyList=[]
      uni.setStorageSync('kw',this.historyList)
    },
    gotoGoodsList(item){
      uni.navigateTo({
        url:'/subpkg/goods_list/goods_list?query='+item
      })
    }
  }
};
</script>
<style lang="scss">
  .history-box {
    padding: 0 5px;
  }
  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }
  .history-list {
    display: flex;
    flex-wrap: wrap;
    .uni-tag {
      margin-top: 5px;
      margin-right: 5px;
    }
  }
</style>
